package com.etymgiko.lyricshere.manager.impl;

import java.util.List;
import java.util.ArrayList;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.etymgiko.lyricshere.manager.ArtistManager;
import com.etymgiko.lyricshere.manager.EMF;
import com.etymgiko.lyricshere.model.Artist;
import com.etymgiko.lyricshere.model.Song;

/**
 *
 * @author Ivan Holub
 */
public class ArtistManagerImpl implements ArtistManager {

    public Artist getArtist(Long id) {
    	EntityManager em = EMF.get().createEntityManager();
		try {
			Artist a = em.find(Artist.class, id);
			return a;
	    } finally {
	        em.close();
	    }
    }

	public List<Artist> getArtists() {
		EntityManager em = EMF.get().createEntityManager();
		try {
			List<Artist> list = em.createQuery("select from " + Artist.class.getName()).getResultList();
			list.size();
	        return list;
	    } finally {
	        em.close();
	    }
	}

	public void saveArtist(Artist artist) {
		EntityManager em = EMF.get().createEntityManager();
		try {
	        em.persist(artist);
	    } finally {
	        em.close();
	    }
	}

	public void updateArtist(Artist artist) {
		EntityManager em = EMF.get().createEntityManager();
		try {
			Artist a = em.find(Artist.class, artist.getId());
			a.setName(artist.getName());
            a.setCode(artist.getCode());
			em.persist(a);
	    } finally {
	        em.close();
	    }
	}

    public void deleteArtist(Long id) {
        EntityManager em = EMF.get().createEntityManager();
        try {
            Query query = em.createQuery("select from " + Song.class.getName() + " s where s.artistId = ?1");
            query.setParameter(1, id);
            List<Song> list = query.getResultList();
            for (Song song : list) {
                em.remove(song);
            }
            Artist a = em.find(Artist.class, id);
            em.remove(a);
        } finally {
            em.close();
        }
    }

    public Artist findArtistByCode(String code) {
        EntityManager em = EMF.get().createEntityManager();
        try {
            Query query = em.createQuery("select from " + Artist.class.getName() + " a where a.code = ?1");
            query.setParameter(1, code);
            Artist artist = null;
            List<Artist> list = query.getResultList();
            if (list.size() > 0) {
                artist = list.get(0);
            }
            return artist;
        } finally {
            em.close();
        }
    }

    public Artist findArtistByName(String name) {
        EntityManager em = EMF.get().createEntityManager();
        try {
            Query query = em.createQuery("select from " + Artist.class.getName() + " a where a.name = ?1");
            query.setParameter(1, name);
            Artist artist = null;
            List<Artist> list = query.getResultList();
            if (list.size() > 0) {
                artist = list.get(0);
            }
            return artist;
        } finally {
            em.close();
        }
    }

}
